package com.jikedaquan.game.jwt.controller;


import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.jikedaquan.game.jwt.entity.Dic;
import com.jikedaquan.game.jwt.entity.Exercise;
import com.jikedaquan.game.jwt.entity.User;
import com.jikedaquan.game.jwt.service.ExerciseService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 修炼记录 前端控制器
 * </p>
 *
 * @author jiake
 * @since 2019-10-31
 */
@Controller
@RequestMapping("/exercise")
public class ExerciseController {

    @Resource
    private ExerciseService exerciseService;

    @GetMapping(path = {"","/","/index","/index.html"})
    public String home(Model model){
        User user = new User();
        user.setId(1);
        Map<String,String> siteInfo = new HashMap<>();
        siteInfo.put("copyright","极客大全");
        siteInfo.put("description","好玩的游戏");
        siteInfo.put("keywords","精武堂,游戏");
        model.addAttribute("siteInfo",siteInfo);
        Exercise exercise = exerciseService.getCurrentExercise(user);
        model.addAttribute("exercise",exercise);
        //修炼状态及时长  修成正果(6小时)
        if (exercise!=null) {
            String statusStr = exerciseService.getExerciseStatusStr(exercise.getStartTime());
            model.addAttribute("exerciseStatus",statusStr);
        }
        return "exercise/home";
    }

    @GetMapping(path = {"/stop","/stop.html"})
    public String stop(Model model){
        User user = new User().setId(1);

        //进行修炼结算
        Exercise exercise = exerciseService.stop(user);
        model.addAttribute("exercise",exercise);
        if (exercise!=null){
            long between = DateUtil.between(exercise.getStartTime(), new Date(), DateUnit.MINUTE);
            String timeStr = between>240?"4小时":
                    DateUtil.formatBetween(between*60*1000, BetweenFormater.Level.MINUTE);
            model.addAttribute("timeStr",timeStr);
        }
        return "exercise/stop";
    }
    @GetMapping(path = {"/start","/start.html"})
    public String start(Model model){

        //获取session中的当前用户
        //暂时使用user 1
        User user = new User();
        user.setId(1);
        boolean isSuccess = exerciseService.start(user);
        model.addAttribute("isSuccess",isSuccess);
        //TODO 体力不足
        return "exercise/start";
    }
}

